"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AudioPlayerWebLayout = void 0;

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _icon = _interopRequireDefault(require("../../../../components/icon"));

var _slider = _interopRequireDefault(require("../../../slider"));

var _spin = _interopRequireDefault(require("../../../spin"));

var _bgView = _interopRequireDefault(require("./bg-view"));

var _web = require("../../../../components/audio/layouts/web");

var _util = require("../util");

var _throttle = _interopRequireDefault(require("../../../../utils/throttle"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// 基于Audio组件重新定义播放样式
var AudioPlayerWebLayout = /*#__PURE__*/function (_AudioWebLayout) {
  _inherits(AudioPlayerWebLayout, _AudioWebLayout);

  var _super = _createSuper(AudioPlayerWebLayout);

  function AudioPlayerWebLayout() {
    var _this;

    _classCallCheck(this, AudioPlayerWebLayout);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "renderContent", function () {
      var clsPrefix = _this.context.clsPrefix;
      var _this$props = _this.props,
          fileName = _this$props.fileName,
          fileSize = _this$props.fileSize;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__wrapper")
      }, /*#__PURE__*/_react.default.createElement("img", {
        alt: "\u56FE\u7247",
        src: "https://imgcache.qq.com/operation/dianshi/other/audio-outlined.d04f8b9380204fe1b1b671f59145695f56cc222d.png",
        className: "".concat(clsPrefix, "-audio-player__wrapper-icon")
      }), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__wrapper-inner")
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__wrapper-inner-fileName")
      }, fileName), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__wrapper-inner-fileSize")
      }, (0, _util.transferFileSize)(fileSize))));
    });

    _defineProperty(_assertThisInitialized(_this), "renderPlayIcon", function () {
      var clsPrefix = _this.context.clsPrefix;
      var disabled = _this.props.disabled;
      var audioImg = _this.state.audioImg; // 按钮样式

      var iconClassName = (0, _classnames.default)("".concat(clsPrefix, "-audio-player__wrapper-inner-btn"), {
        'is-disabled': disabled || audioImg === 'CloseOutlined'
      });

      if (disabled) {
        return /*#__PURE__*/_react.default.createElement(_icon.default, {
          name: 'CloseOutlined',
          className: iconClassName,
          size: 12
        });
      }

      if (audioImg === 'iconLoading') {
        return /*#__PURE__*/_react.default.createElement(_spin.default, {
          className: iconClassName,
          type: "spinner",
          size: 12
        });
      }

      return /*#__PURE__*/_react.default.createElement(_icon.default, {
        onClick: _this.playOrStopAudio,
        name: audioImg,
        className: iconClassName,
        size: 12
      });
    });

    _defineProperty(_assertThisInitialized(_this), "renderOperation", function () {
      var clsPrefix = _this.context.clsPrefix;
      var _this$props2 = _this.props,
          onDelete = _this$props2.onDelete,
          onLink = _this$props2.onLink,
          onDownload = _this$props2.onDownload,
          onPlay = _this$props2.onPlay,
          disabled = _this$props2.disabled;
      var audioImg = _this.state.audioImg;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__right")
      }, _this.renderPlayIcon(), onLink && /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__right-link"),
        onClick: function onClick() {
          return onLink();
        }
      }, "\u94FE\u63A5"), onDownload && /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__right-download"),
        onClick: function onClick() {
          return onDownload();
        }
      }, "\u4E0B\u8F7D"), onDelete && /*#__PURE__*/_react.default.createElement(_icon.default, {
        name: "DeleteOutlined",
        className: "".concat(clsPrefix, "-audio-player__right-delete"),
        onClick: function onClick() {
          onDelete();
        }
      }));
    });

    _defineProperty(_assertThisInitialized(_this), "progressChange", (0, _throttle.default)(function (percentage) {
      var currentTime = _this.state.duration * percentage / 100;

      _this.setState({
        currentTime: currentTime
      });

      _this.seek(currentTime);
    }));

    _defineProperty(_assertThisInitialized(_this), "seekTimeout", null);

    _defineProperty(_assertThisInitialized(_this), "seek", function (currentTime) {
      clearTimeout(_this.seekTimeout);
      _this.seekTimeout = setTimeout(function () {
        _this.state.audioCtx.seek(currentTime);
      }, 100);
    });

    _defineProperty(_assertThisInitialized(_this), "renderProgress", function () {
      var clsPrefix = _this.context.clsPrefix;
      var _this$state = _this.state,
          currentTime = _this$state.currentTime,
          duration = _this$state.duration;
      var percent = currentTime * 100 / duration || 0;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-audio-player__progress")
      }, /*#__PURE__*/_react.default.createElement(_slider.default, {
        value: percent,
        step: 0.1,
        formatter: function formatter(value) {
          return null;
        },
        onChange: _this.progressChange
      }));
    });

    return _this;
  }

  _createClass(AudioPlayerWebLayout, [{
    key: "render",
    value: function render() {
      var clsPrefix = this.context.clsPrefix;
      var audioImg = this.state.audioImg;
      var _this$props3 = this.props,
          style = _this$props3.style,
          className = _this$props3.className;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-audio-player"), className),
        style: style
      }, /*#__PURE__*/_react.default.createElement(_bgView.default, {
        leftChildren: this.renderContent(),
        rightChildren: this.renderOperation(),
        bottomChildren: this.renderProgress()
      }));
    }
  }]);

  return AudioPlayerWebLayout;
}(_web.AudioWebLayout);

exports.AudioPlayerWebLayout = AudioPlayerWebLayout;